package main;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class OpenPublicationDialog extends JDialog implements ActionListener{

    private static OpenPublicationDialog dialog;
    private JList list;
    private JTextArea previewArea;
    private JButton openButton, closeButton;
    private static String value = "";

	public static String showDialog(Component frameComp, Component locationComp, String[] values){
		Frame frame = JOptionPane.getFrameForComponent(frameComp);

		dialog = new OpenPublicationDialog(frame, locationComp, values);

		dialog.setVisible(true);
		return value;
	}


	private OpenPublicationDialog(Frame frame, Component locationComp, String[] values){
		super(frame, "Open Publication", true);

		this.setResizable(false);
		init(values);
	}

	private void init(String[] values){
        JPanel mainPanel = new JPanel();

		list = new JList(values);
		list.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        JScrollPane listScroller = new JScrollPane(list);

        openButton = new JButton();
        openButton.setText("Open");
        openButton.addActionListener(this);

        closeButton = new JButton();
        closeButton.setText("Cancel");
        closeButton.addActionListener(this);

        previewArea = new JTextArea();
        previewArea.setColumns(20);
        previewArea.setRows(5);
        previewArea.setEditable(false);
        previewArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listScroller, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(openButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(previewArea, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(previewArea, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(openButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(listScroller, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        Container contentPane = getContentPane();
        contentPane.add(mainPanel);
        pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
        if (e.getSource() == openButton) {
            value = (String)(list.getSelectedValue());
        }
        dialog.setVisible(false);
	}
}
